from django.shortcuts import render
from ZKces.models import *
from rest_framework.response import Response
from django.contrib.auth.hashers import make_password
from ZKces.serializers import *
from django.core.paginator import Paginator
# Create your views here.
from django.db.models import F
from rest_framework.views import APIView
import uuid,os
from qiniu import Auth, put_file, etag
import qiniu.config
from MD_django.settings import BASE_DIR

def pull_qiniu(pic_name):
    access_key = 'LYyYQzkYNdzswDd84W0m5z5YzBMBDVaOziK8EbKO'
    secret_key = '2Cejvu_Ab3MRnZrm3ZAsJZK46EAz-5yxKWQL5lh9'
    q = Auth(access_key, secret_key)
    bucket_name = 'djangol'
    key = pic_name
    # 上传文件到七牛后， 七牛将文件名和文件大小回调给业务服务器。
    policy = {
        'callbackUrl': 'http://your.domain.com/callback.php',
        'callbackBody': 'filename=$(fname)&filesize=$(fsize)'
    }
    token = q.upload_token(bucket_name, key, 3600, policy)
    localfile = BASE_DIR+"/"+pic_name
    ret, info = put_file(token, key, localfile)
    print(info)
    url = "http://q8i08vb02.bkt.clouddn.com/"
    return url + pic_name

class FenView(APIView):
    def post(self,request):
        data = request.data
        name = data["name"]
        try:
            FenLei.objects.create(name=name)
            res_data={
                "code":200,
                "msg":"添加成功"
            }
            return Response(res_data,status=200)
        except:
            res_data={
                "code":500,
                "msg":"添加失败"
            }
            return Response(res_data,status=200)
    def get(self,request):
        data = request.query_params
        try:
            fenleiall = FenLei.objects.all()
            fenres = FenLeiRES(fenleiall,many=True)
            data = fenres.data
            res_data={
                "code":200,
                "msg":"获取成功",
                "data":data
            }
            return Response(res_data,status=200)
        except:
            res_data = {
                "code": 200,
                "msg":"获取失败"
            }
            return Response(res_data, status=200)

class GoodsShow(APIView):
    def get(self,request):
        data = request.query_params
        page = data["page"]
        print(page)
        goodsall = Goods.objects.all()
        goods_fy = Paginator(goodsall,3)
        goods_info = goods_fy.page(page)
        goods_list = [i for i in goods_fy.page_range]
        goods_data = GoodsRES(goods_info,many=True)
        data = goods_data.data
        print(data)
        res_data={
            "code":200,
            "msg":"展示成功",
            "goods_list":goods_list,
            "data":data,
            "page_info":goods_info.number,
        }

        return Response(res_data,status=200)

class Goodscer(APIView):
    def post(self,request):
        data = request.data
        print(data)
        try:
            uuid_str = str(uuid.uuid4())
            pic_name = uuid_str + data["img"].name
            with open(pic_name,"wb") as f:
                for i in data["img"].chunks():
                    f.write(i)
            pic_name = pull_qiniu(pic_name)
            data["img"] = pic_name
            print(data["img"])
            Goods.objects.create(name=data["name"],price=data["price"],
                                 fenlei_id=data["fenlei"],img=data["img"],detll=data["detll"])
            res_data={
                "code":200,
                "msg":"成功"
            }
            return Response(res_data,status=200)
        except:
            res_data={
                "code":500,
                "msg":"失败"
            }
            return Response(res_data,status=200)

class ClassView(APIView):
    def post(self,request):
        data =request.data
        try:
            Class.objects.create(name=data["name"])
            res_data={
                "code":200,
                "msg":"品牌添加成功"
            }
            return Response(res_data,status=200)
        except:
            res_data={
                "code":500,
                "msg":"类型添加失败"
            }
            return Response(res_data,status=200)
    def get(self,request):
        data = request.query_params
        cla_show = Class.objects.all()
        cla_res = ClassRES(cla_show,many=True)
        data = cla_res.data
        res_data={
            "code":200,
            "msg":"查找成功",
            "data":data
        }
        return Response(res_data,status=200)

class ComView(APIView):
    def post(self,request):
        data = request.data
        print(data)
        try:
            uuid_pic = str(uuid.uuid4())
            pic_name = uuid_pic + data["img"].name
            with open(pic_name,"wb") as f:
                for i in data["img"].chunks():
                    f.write(i)
            pic_name = pull_qiniu(pic_name)
            data["img"] = pic_name
            print(data["img"])
            Computer.objects.create(name=data["name"],price=data["price"],
                                    size=data["size"],guige=data["guige"],
                                    img=data["img"],clas_id=data["ssa"])
            res_data={
                "code":200,
                "msg":"添加电脑成功"
            }
            return Response(res_data,status=200)
        except:
            res_data={
                "code":500,
                "msg":"添加电脑失败"
            }
            return Response(res_data,status=200)

class ComShowView(APIView):
    def get(self,request):
        data = request.query_params
        page = data["page"]
        comall = Computer.objects.all()
        comfy = Paginator(comall,3)
        com_info = comfy.page(page)
        comlist = [i for i in comfy.page_range]
        comres = ComRES(com_info,many=True)
        data = comres.data
        res_data={
            "code":200,
            "msg":"分页成功",
            "data":data,
            "page_info":com_info.number,
            "fylist":comlist
        }
        return Response(res_data,status=200)

class ClaView(APIView):
    def post(self,request):
        data = request.data
        print(data)
        try:
            coms = Computer.objects.filter(clas=data).filter()
            comsres = ComRES(coms,many=True)
            data = comsres.data
            print(data)
            res_data={
                "code":200,
                "msg":"获取成功",
                "data":data,
            }
            return Response(res_data,status=200)
        except:
            return Response({"code":500,"msg":"获取失败"},status=200)

class UserView(APIView):
    def get(self,request):
        data = request.query_params
        print(data)
        name =data["a"]
        # print(data)
        # name = data["name"]
        # print(name)
        try:
            User.objects.get(username=name)
            res_data={
                "code":200,
                "msg":"登陆成功",
                "data":name
            }
            return Response(res_data,status=200)
        except:
            res_data={
                "code":500,
                "msg":"登陆失败"
            }
            return Response(res_data,status=200)

class XinWenView(APIView):
    def get(self,request):
        data = request.query_params
        try:
            xinall = XinWen.objects.all()
            xinres = XinWenRes(xinall,many=True)
            data = xinres.data
            res_data={
                "code":200,
                "msg":"获取新闻信息成功",
                "data":data
            }
            return Response(res_data,status=200)
        except:
            res_data={
                "code":500,
                "msg":"获取新闻信息失败"
            }
            return Response(res_data,status=200)

class XinPingView(APIView):
    def post(self,request):
        data = request.data
        print(data)
        try:
            xinall = XinWen.objects.filter(id=data).filter()
            xinres = XinWenRes(xinall, many=True)
            data = xinres.data
            res_data={
                "code":200,
                "msg":"获取成功",
                "data":data
            }
            return Response(res_data,status=200)
        except:
            res_data = {
                "code": 500,
                "msg": "获取失败",
            }
            return Response(res_data, status=200)

